//网站管理部分

const fs = require('fs');
const path = require('path');

const articleSchema = require('../../schemas/articleSchema.js');//文章表
const typeSchema = require('../../schemas/typeSchema.js');//分类表
const tagSchema = require('../../schemas/tagSchema.js');//tag标签表
const messageSchema = require('../../schemas/messageSchema.js');//message表


/*不需要操作数据库的方法*/
//添加web的tags
exports.addWebTags = async ctx=>{
	let {id,name} = ctx.request.body;
	let tags = GetWebTags();
	tags.push({
		id,
		name
	});
	fs.writeFileSync('./data/requiredJSON/webTags.json',JSON.stringify(tags));
	ctx.body = {
		status:true
	};
};
//添加web的文章分类
exports.addWebType = async ctx=>{
	let {id,name} = ctx.request.body;
	let types = GetWebTypes();
	types.push({
		id,
		name
	});
	fs.writeFileSync('./data/requiredJSON/webTypes.json',JSON.stringify(types));
	ctx.body = {
		status:true
	}
};
//保存图片
exports.saveWebImg = async ctx=>{
	let {data,name,type} = ctx.request.body;
	let markData = fs.readFileSync('./data/requiredJSON/webMark.json');
	markData = JSON.parse(markData);
	markData.lastImgId += 1;
	fs.writeFileSync('./data/requiredJSON/webMark.json',JSON.stringify(markData));
	fs.writeFileSync(`./data/imgs/${markData.lastImgId}.${type}`, Buffer.from(data,'base64'));
	ctx.body = {
		data:{
			href:`${markData.lastImgId}.${type}`
		}
	}
};
//获取图片链接
exports.getWebImgs = async ctx=>{
	let data = fs.readdirSync('./data/imgs');
	data = data.map(item=>{
		return item.split('.');
	});
	data.sort((a,b)=>a[0] - b[0]);
	data = data.map(item=>{
		return item[0] + '.' + item[1];
	});
	ctx.body = {
		data
	}
};
//删除图片
exports.delImg = async ctx=>{
	let {name} = ctx.request.body;
	let path = './data/imgs/' + name;
	fs.unlinkSync(path);
	ctx.body = {
		status:true
	}
};

/*需要操作数据库的方法*/
//删除web的tags
exports.delWebTags = async ctx=>{
	let {id} = ctx.request.body;
	let types =  GetWebTags();
	tags = types.filter(item=>{
		return item.id !== id;
	});
	fs.writeFileSync('./data/requiredJSON/webTags.json',JSON.stringify(tags));
	let result = await tagSchema.deleteMany({tagId:id});
	ctx.body = {
		status:true
	}
};
//修改web的文章分类
exports.changeWebType = async ctx=>{
	let {id,name} = ctx.request.body;
	let types =  GetWebTypes();
	types = types.map(item=>{
		let newId = item.id;
		let newName = item.name;
		if(newId === id){
			newName = name
		}
		return {
			id:newId,
			name:newName
		};
	});
	fs.writeFileSync('./data/requiredJSON/webTypes.json',JSON.stringify(types));
	let result = await typeSchema.update({typeId:id},{$set:{typeName:name}});
	ctx.body = {
		status:true
	};
};
//删除web的文章分类
exports.delWebType = async ctx=>{
	let {id} = ctx.request.body;
	let types =  GetWebTypes();
	types = types.filter(item=>{
		return item.id !== id;
	});
	fs.writeFileSync('./data/requiredJSON/webTypes.json',JSON.stringify(types));
	let result = await typeSchema.deleteMany({
		articleId:id
	});
	ctx.body = {
		status:true
	}
};
//保存新web文章
exports.publishWebArticle = async ctx=>{
	let {article,title,description,keywords,tags,types,imgUrl,imgAlt,imgTitle} = ctx.request.body;
	//获取并修改最新文章ID
	let markData = fs.readFileSync('./data/requiredJSON/webMark.json');
	markData = JSON.parse(markData);
	markData.lastArticleId += 1;
	fs.writeFileSync('./data/requiredJSON/webMark.json',JSON.stringify(markData));
	//存储新文章
	let time = new Date().toLocaleDateString();
	time = time.replace(/\//g,'-');
	let result = await articleSchema.create({
		id:markData.lastArticleId,
		title,
		description,
		keywords,
		time,
		views:0,
		messages:0,
		textContent:article.text,
		htmlContent:article.html,
		imgUrl,
		imgAlt,
		imgTitle
	});
	//更新tag表
	for(let i = 0;i < tags.length;i++){
		result = await tagSchema.create({
			articleId:markData.lastArticleId,
			articleName:title,
			tagId:tags[i].id,
			tagName:tags[i].name
		});
	}
	//更新type表
	for(let i = 0;i < types.length;i++){
		result = await typeSchema.create({
			articleId:markData.lastArticleId,
			articleName:title,
			typeId:types[i].id,
			typeName:types[i].name
		});
	}
	ctx.body = {
		data:{
			id:markData.lastArticleId
		}
	}
};
//删除文章评论
exports.delArticleMessage = async ctx=>{
	let {id,articleId} = ctx.request.body;
	await messageSchema.deleteOne({_id:id});
	let {messages} = await articleSchema.findOne({id:articleId});
	messages = messages - 1;
	await articleSchema.update({id:articleId},{$set:{messages}});
	ctx.body = {
		status:true
	}
};
//删除文章
exports.delArticleById = async ctx=>{
	let {id} = ctx.request.body;
	await articleSchema.deleteOne({id});
	await messageSchema.deleteMany({articleId:id});
	await tagSchema.deleteMany({articleId:id});
	await typeSchema.deleteMany({articleId:id});
	ctx.body = {
		status:true
	}
};
//更新文章
exports.updateArticle = async ctx=>{
	let {id,title,description,keywords,imgUrl,imgTitle,imgAlt} = ctx.request.body;
	await articleSchema.update({id},{$set:{
			title,
			description,
			keywords,
			imgUrl,
			imgTitle,
			imgAlt
		}});
	ctx.body = {
		status:true
	}
};
//给文章添加tag
exports.addArticleTag = async ctx=>{
	let {id,tagId,tagName,title} = ctx.request.body;
	await tagSchema.create({
		articleId:id,
		tagId,
		tagName,
		articleName:title
	});
	ctx.body = {
		status:true
	}
};
//给文章添加分类
exports.addArticleType = async ctx=>{
	let {id,typeId,typeName,title} = ctx.request.body;
	await typeSchema.create({
		articleId:id,
		typeId,
		typeName,
		articleName:title
	});
	ctx.body = {
		status:true
	}
};
//删除文章tag
exports.delArticleTag = async ctx=>{
	let {id,tagId} = ctx.request.body;
	await tagSchema.deleteOne({
		articleId:id,
		tagId
	});
	ctx.body = {
		status:true
	}
};
//删除文章分类
exports.delArticleType = async ctx=>{
	let {id,typeId} = ctx.request.body;
	await typeSchema.deleteOne({
		articleId:id,
		typeId
	});
	ctx.body = {
		status:true
	}
};

function GetWebTypes(){
	let result = fs.readFileSync('./data/requiredJSON/webTypes.json');
	let types = JSON.parse(result.toString());
	return types;
}
function GetWebTags(){
	let result = fs.readFileSync('./data/requiredJSON/webTags.json');
	let tags = JSON.parse(result.toString());
	return tags;
}
